import { Controller } from "../../mvc/core/Controller";
import { IDramaLottery } from "../../../modules/interfaces/IDramaLottery";
import { IView } from "../../mvc/interfaces/IView";
import { DramaLotteryModule } from "../../../modules/DramaLotteryModule";
import { DramaLotteryHintBtn } from "../view/dramaLottery/DramaLotteryHintBtn";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameConst } from "../../component/gconst/GameConst";
import { ExplorerRumorData } from "../view/treasure/ExplorerRumorData";
import { GameProxy } from "../mvc/GameProxy";
import { SPublicNotice } from "../../../Message/Public/SPublicNotice";
import { EShowArea } from "../../../Message/Public/EShowArea";
import { Cache } from "../cache/Cache";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { Alert } from "../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { SDramaLotteryInfo } from "../../../Message/Game/SDramaLotteryInfo";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { SSeqLotteryItemInfo } from "../../../Message/Game/SSeqLotteryItemInfo";
type int = number;
//class DramaLotteryController
    
    export  class DramaLotteryController extends Controller
    {
       

        public get dramaLottery(): IDramaLottery
        {
            if (null == this._dramaLottery) 
            {
                this.initView();
            }
            return this._dramaLottery;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (this._dramaLottery == null) 
            {
                this._dramaLottery = new DramaLotteryModule();
            }
            return this._dramaLottery;
        }

        public get dramaLotteryBtn(): DramaLotteryHintBtn
        {
            if (!this._dramaLotteryBtn) 
            {
                this._dramaLotteryBtn = new DramaLotteryHintBtn();
            }
            return this._dramaLotteryBtn;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.OpenDramaLotteryModule, this.onOpenDramaLotteryModule);
            Dispatcher.addEventListener(EventName.CloseDramaLotteryModule, this.onCloseDramaLotteryModule);
            NetDispatcher.addCmdListener(ServerCommand.DramaLotteryCommand, this.onDramaLotteryCommand);
            Dispatcher.addEventListener(EventName.DramaLotteryGoRefreshItem, this.onDramaLotteryGoRefreshItem);
            Dispatcher.addEventListener(EventName.DramaLotteryGoBuyItem, this.onDramaLotteryGoBuyItem);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.onLevelChange);
            Dispatcher.addEventListener(EventName.TreasureExplorerRumorAdd, this.onExplorerRumorAddHandler);
            Dispatcher.addEventListener(EventName.DramaLotteryGetExplorerRecords, this.onGetExplorerRecordsHandler);
            Dispatcher.addEventListener(EventName.TreasureGetRecordsSuccess, this.onGetRecordsSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveDramaLotteryBuyAdd, this.onActiveDramaLotteryBuyAddHandler);
            return;
        }

        /* internal  */onGetRecordsSuccessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data["type"] as int;
            if (loc1 == GameConst.DramaLotteryExplorerType) 
            {
                loc2 = arg1.data["records"] as Array<any>;
                ExplorerRumorData.noticeMap[loc1] = loc2;
                if (!(this._dramaLottery == null) && !this._dramaLottery.isHide) 
                {
                    this._dramaLottery.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onGetExplorerRecordsHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (ExplorerRumorData.getNoticeArray(GameConst.DramaLotteryExplorerType) == null) 
            {
                GameProxy.sceneProxy.getExplorerRecords(GameConst.DramaLotteryExplorerType);
            }
            return;
        }

        /* internal  */onExplorerRumorAddHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPublicNotice;
            if (loc1.area & EShowArea._EShowAreaExplorer && loc1.code == GameConst.DramaLotteryExplorerType) 
            {
                ExplorerRumorData.setNoticeMap([loc1], loc1.code);
                if (!(this._dramaLottery == null) && !this._dramaLottery.isHide) 
                {
                    this._dramaLottery.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onLevelChange(arg1: Object=null): void
        {
            this.showDramaLotteryBtn();
            return;
        }

        /* internal  */onDramaLotteryNoticeAdd(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPublicNotice;
            if (this._dramaLottery && !this._dramaLottery.isHide) 
            {
                this.dramaLottery.updateAllBuyHistroy(loc1);
            }
            return;
        }

        /* internal  */onCloseDramaLotteryModule(arg1: DataEvent): void
        {
            if (this._dramaLottery && !this._dramaLottery.isHide) 
            {
                this._dramaLottery.hide();
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            if (this._dramaLottery && !this._dramaLottery.isHide) 
            {
                this.dramaLottery.updateMoney();
            }
            return;
        }

        /* internal  */onDramaLotteryGoBuyItem(arg1: DataEvent): void
        {
            var loc5=null;
            var loc6=null;
            var loc7=false;
            var loc1=arg1.data.nameTx as string;
            var loc2=arg1.data.nowPrice as int;
            var loc3=arg1.data.index as int;
            this._index = loc3;
            var loc4;
            if ((loc4 = Cache.instance.role.money.gold + Cache.instance.role.money.goldBind) < loc2) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70658));
                return;
            }
            loc5 = HTMLUtil.addColor("1", "#FF0000");
            loc6 = Language.getStringByParam(70675, loc5, loc1, loc2);
            if (loc7 = !PulseSharedObject.isTodayNotTips("DramaLotteryShopBuy")) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.show(loc6, null, Alert.OK | Alert.CANCEL, null, this.onBtnClickHandlerI);
            }
            else 
            {
                GameProxy.roleProxy.buyLotteryItem(loc3);
            }
            return;
        }

        /* internal  */onBtnClickHandlerI(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                Alert.resetToDefault();
                GameProxy.roleProxy.buyLotteryItem(this._index);
            }
            if (arg2) 
            {
                PulseSharedObject.save("DramaLotteryShopBuy", true);
            }
            return;
        }

        /* internal  */onDramaLotteryGoRefreshItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.roleProxy.refreshLottery(loc1);
            return;
        }

        /* internal  */onDramaLotteryCommand(arg1: Object): void
        {
            this._sDramaLotteryInfo = arg1 as SDramaLotteryInfo;
            if (this._dramaLottery && !this._dramaLottery.isHide) 
            {
                this._dramaLottery.updateInfo(this._sDramaLotteryInfo);
            }
            this.showDramaLotteryBtn();
            return;
        }

        /* internal  */showDramaLotteryBtn(): void
        {
            if (cache.role.entityInfo.level < GameConst.DramaLotteryLimit) 
            {
                this.dramaLotteryBtn.hide();
            }
            else if (this._sDramaLotteryInfo) 
            {
                this.dramaLotteryBtn.show();
            }
            return;
        }

        /* internal  */onOpenDramaLotteryModule(arg1: DataEvent): void
        {
            if (Cache.instance.role.entityInfo.level < GameConst.DramaLotteryLimit) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70755));
                return;
            }
            if (this.dramaLottery.isHide) 
            {
                if (this._sDramaLotteryInfo) 
                {
                    this.dramaLottery.show();
                    this.dramaLottery.updateMoney();
                    this.dramaLottery.updateInfo(this._sDramaLotteryInfo);
                }
            }
            else 
            {
                this.dramaLottery.hide();
            }
            return;
        }

        /* internal  */onActiveDramaLotteryBuyAddHandler(arg1: SActiveToClient): void
        {
            if (this._dramaLotteryBtn && !this._dramaLotteryBtn.isHide) 
            {
                this._dramaLotteryBtn.updateActiveToClientInfo(arg1);
            }
            return;
        }

        private /* var */_dramaLottery: IDramaLottery;

        private /* var */_sDramaLotteryInfo: SDramaLotteryInfo;

        private /* var */_seqLotteryItemInfo: SSeqLotteryItemInfo;

        private /* var */_dramaLotteryBtn: DramaLotteryHintBtn;

        private /* var */_index: int;
    }
